﻿using UnityEngine;

public class MathVector 
{
    /// <summary>
    /// 自身旋转，这个方法不太会用
    /// </summary>
    /// <param name="fromTransform">旋转的模型</param>
    /// <param name="toDirection">旋转到的单位向量</param>
    /// <returns></returns>
    Quaternion FromToRotationSong(Transform fromTransform, Vector3 toDirection)
    {
        return (Quaternion.FromToRotation(fromTransform.forward, toDirection) * fromTransform.rotation);
    }
    /// <summary>
    /// 旋转到指定的向量
    /// </summary>
    /// <param name="toDirection">旋转到的单位向量</param>
    /// <param name="selfDirection">自身的方向（不传默认是up）</param>
    /// <returns></returns>
    Quaternion LookRotationSong(Vector3 toDirection, Vector3 selfDirection)
    {
        return Quaternion.LookRotation(toDirection, selfDirection);
    }
    Quaternion LookRotationSong(Vector3 toDirection)
    {
        return Quaternion.LookRotation(toDirection);
    }
    /// <summary>
    /// 旋转到指定的角度(在updata里面实时调用)
    /// </summary>
    /// <param name="from">自身的四元数</param>
    /// <param name="to">目标四元数</param>
    /// <param name="step">旋转的角速度</param>
    /// <returns></returns>
    Quaternion FromToRotationSong(Quaternion from, Quaternion to, float step)
    {
        step = step * Time.deltaTime;
        return Quaternion.RotateTowards(from, to, step);
    }
    /// <summary>
    /// 返回两个四元数之间的角度
    /// </summary>
    /// <param name="self"></param>
    /// <param name="target"></param>
    /// <returns></returns>
    float AngleSong(Quaternion self, Quaternion target)
    {
        return Quaternion.Angle(self, target);
    }
    /// <summary>
    /// 返回两个向量之间的角度
    /// </summary>
    /// <param name="self"></param>
    /// <param name="target"></param>
    /// <returns></returns>
    float AngleSong(Vector3 self, Vector3 target)
    {

        float angle = Vector3.Angle(self, target);
        return angle;
        //float dot = Vector3.Dot(self, target);
        //float angle = Mathf.Acos(dot / (self.magnitude * target.magnitude))* Mathf.Rad2Deg;

    }

    //void GetAngleAxis()
    //{
    //    float angle = 0.0f;
    //    Vector3 axis = Vector3.zero;
    //    transform.rotation.ToAngleAxis(out angle, out axis);
    //    Debug.Log(angle + "***" + axis);
    //}
    /// <summary>
    /// 旋转角度
    /// </summary>
    /// <param name="Transform">旋转物体</param>
    /// <param name="Angle">旋转角度</param>
    /// <param name="Vector3">旋转轴</param>
    void AngleAxisSong(Transform Transform,float Angle,Vector3 Vector3)
    {
        Transform.rotation = Quaternion.AngleAxis(Angle, Vector3);
    }

    /// <summary>
    /// 旋转到目标向量
    /// </summary>
    /// <param name="current">自身向量</param>
    /// <param name="target">目标向量</param>
    /// <param name="maxRadiansDelta">一帧最大旋转角度（弧度）</param>
    /// <param name="maxMagnitudeDelta">振幅</param>
    /// <returns></returns>
    public Vector3 RotateTowards(Vector3 current, Vector3 target, float maxRadiansDelta, float maxMagnitudeDelta)
    {
        return Vector3.RotateTowards(current, target, maxRadiansDelta, maxMagnitudeDelta);
    }
}
